# Hunger Games Submission
# First time with Python, just copy-paste the sample code :P

# Arifin Luthfi P
# Indonesia
# http://arifin.himaci.com

class Player:
    def __init__(self):
        self.food = 0
        self.reputation = 0

        self.top_10_reputations = []
        self.minimum_hunt_ratio_for_top_10 = 0
        self.sweet_reputation = 0

        self.last_player_reputations = []
        self.hunt_data_counter = {}
        self.slack_data_counter = {}

    def hunt_choices(self, round_number, current_food, current_reputation, m, player_reputations):
        # Cointainer
        hunt_decisions = []

        if round_number <= 10:
            # Early round, I think people use this as "data training". I'm going to slack to mess those people analytics!
            for i in range(len(player_reputations)):
                decision = 's'
                # Some hunt to prevent people think I'm always slack
                if i < 3: decision = 'h'
                # Append to the list
                hunt_decisions.append(decision)

        elif round_number <= 50:
            # Advanced round, let's always hunt to gain data training (for every reputation I between 0 - about 0.8)
            for i in range(len(player_reputations)):
                decision = 'h'
                # If I found people who always slack (rep = 0), why bother to hunt?
                if player_reputations[i] == 0: decision = 's'
                # Append to the list
                hunt_decisions.append(decision)

        else:
            # Next next round, use the trained data as hunt decision
            if len(player_reputations) == 1:
                # Nah, if 1 player remaining, I'll just slack
                hunt_decisions.append('s')
            else:
                # WTF SHOULD I DO HERE???
                # Read the 'get_decision' function
                for i in range(len(player_reputations)):
                    decision = self.get_decision(current_reputation, player_reputations[i])
                    hunt_decisions.append(decision)

        # Save the data
        self.food = current_food
        self.reputation = current_reputation;
        self.last_player_reputations = player_reputations[:]

        return hunt_decisions

    def hunt_outcomes(self, food_earnings):
        # Note people hunt/slack ratio agains me with current reputation
        # Hunt ratio is used to calculate sweet reputation
        # Sweet reputation is average top 10 of my reputations based on others hunt_ratio
        # Sweet reputation is used when I'm in doubt to hunt or slack
        hunt_counter = food_earnings.count(0) + food_earnings.count(1)
        slack_counter = food_earnings.count(-3) + food_earnings.count(-2)
        hunt_ratio = float(hunt_counter)/float(hunt_counter+slack_counter)

        # Track top 10 my reputation
        if len(self.top_10_reputations) < 10:
            self.top_10_reputations.append(self.reputation)
            self.top_10_reputations.sort()
            if len(self.top_10_reputations) == 1:
                self.minimum_hunt_ratio_for_top_10 = hunt_ratio
            elif hunt_ratio < self.minimum_hunt_ratio_for_top_10:
                self.minimum_hunt_ratio_for_top_10 = hunt_ratio

        elif hunt_ratio > self.minimum_hunt_ratio_for_top_10:
                self.top_10_reputations[0] = self.reputation
                self.top_10_reputations.sort()
                self.minimum_hunt_ratio_for_top_10 = hunt_ratio

        # Calculate sweet reputation
        self.sweet_reputation = float(sum(self.top_10_reputations))/float(len(self.top_10_reputations))

        # Save the hunt result to the dictionary as data learning
        # Earning 0 or 1 means other hunt
        # Earning -3 or -2 means other slack
        for i in range(len(food_earnings)):
            # Key in dictionary
            data_key = "mine_%.2f_other_%.2f" % (round(self.reputation,2), round(self.last_player_reputations[i],2))
            # If people hunt, increase hunt data, otherwise, increase slack data
            if food_earnings[i] == 0 or food_earnings[i] == 1:
                self.hunt_data_counter[data_key] = self.hunt_data_counter.get(data_key, 0) + 1
            else:
                self.slack_data_counter[data_key] = self.slack_data_counter.get(data_key, 0) + 1

        # Add food
        self.food += sum(food_earnings)

    def round_end(self, award, m, number_hunters):
        # Why bother with this 'M' thing anyway?
        # Everyone will get the same prize, no matter what we do for that round
        # Nah, I'm just to lazy to find the connection LOL

        # Add food
        self.food += award
    
    # ------------------
    # Some utility stuff
    # ------------------

    def get_decision(self, self_reputation, enemy_reputation):
        # Default slacking
        decision = 's'

        # Get data from data training
        data_key = "mine_%.2f_other_%.2f" % (round(self_reputation,2), round(enemy_reputation,2))
        hunt_counter = self.hunt_data_counter.get(data_key, 0)
        slack_counter = self.slack_data_counter.get(data_key, 0)

        if hunt_counter + slack_counter == 0:
            # If no data, process to gain sweet_reputation
            if self.reputation < self.sweet_reputation:
                decision = 'h'
            else:
                decision = 's'
        else:
            # Hunt based on hunt ratio
            hunt_ratio = float(hunt_counter)/float(hunt_counter+slack_counter)
            if hunt_ratio < 0.4:
                decision = 's'
            else:
                # If people is about to hunt, process to gain sweet_reputation, profit?
                if self.reputation < self.sweet_reputation:
                    decision = 'h'
                else:
                    decision = 's'

        return decision